package demo4;

import javax.activation.CommandObject;
import java.util.Arrays;
import java.util.Comparator;

class Student implements Comparable<Student> {
    public String name;
    public int age;

    public Student(String name,int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    //类于类比较时会强转为Comparable类，然后用这个类的接口去比较
    //compareTo的功能有限，所以我们一般比较时需要自己去实现
    //但是自己实现的只能比较一个，如果要比较name就不行了
    public int compareTo(Student o){
        if(this.age - o.age > 0){
            return 1;
        }else if(this.age - o.age < 0){
            return -1;
        }else{
            return 0;
        }
    }
}
class AgeComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) { return o1.age - o2.age;}
}

class NameComparator implements Comparator<Student>{
    @Override
    public int compare (Student o1, Student o2){ return  o1.name.compareTo(o2.name);}
}

public class Test1 {
    public static void main1(String[] args) {
        int[] array = {1,4,2,7,3,8,5,6};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }
    public static void main2(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("bit",10);
        students[1] = new Student("hello",40);
        students[2] = new Student("abc",5);

        Arrays.sort(students);
        System.out.println(Arrays.toString(students));
        //无法打印，程序直接抛异常
    }
    public static void main3(String[] args) {
        String[] strings = {"abc","hello","bcd"};
        Arrays.sort(strings);
        System.out.println(Arrays.toString(strings));
    }
    public static void main4(String[] args) {
        Student student1 = new Student("bit",10);
        Student student2 = new Student("hello",40);

        if(student1.compareTo(student2) > 0) {
            System.out.println("student1 > student2");
        }else {
            System.out.println("student1 < student2");
        }
    }
    public static void main5(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("bit",10);
        students[1] = new Student("hello",40);
        students[2] = new Student("gbc",5);

        //AgeComparator ageComparator = new AgeComparator();
        NameComparator nameComparator = new NameComparator();
        Arrays.sort(students,nameComparator);

        System.out.println(Arrays.toString(students));
    }
    public static void main6(String[] args) {
        Student student1 = new Student("bit",10);
        Student student2 = new Student("hello",40);

        AgeComparator ageComparator = new AgeComparator();

        if(ageComparator.compare(student1,student2) > 0) {
            System.out.println("student1 > student2");
        }else {
            System.out.println("student1 < student2");
        }
    }
    public static void bubbleSort(Comparable[] array) {
        for (int i = 0; i < array.length-1; i++) {
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j].compareTo(array[j+1]) > 0) {
                    Comparable tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                }
            }
        }
    }
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("bit",10);
        students[1] = new Student("hello",40);
        students[2] = new Student("gbc",5);

        bubbleSort(students);

        System.out.println(Arrays.toString(students));

    }
}
